package com.ruoyi.eqmonitor.dispose.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.eqmonitor.dispose.domain.ExteriorSiteAlarm;
import com.ruoyi.eqmonitor.dispose.dtos.ExteriorSiteAlarmVo;
import com.ruoyi.eqmonitor.dispose.mapper.ExteriorSiteAlarmMapper;
import com.ruoyi.eqmonitor.dispose.service.IExteriorSiteAlarmService;
import com.ruoyi.eqmonitor.dtos.CockpitRiverwayAlarmDto;
import com.ruoyi.eqmonitor.dtos.OptDeviceAlarmDto;
import com.ruoyi.eqmonitor.dtos.OptPatrolEventDPY;
import com.ruoyi.eqmonitor.mapper.OptPatrolEventMapper;
import com.ruoyi.eqmonitor.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 微站报警Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-11
 */
@Service
public class ExteriorSiteAlarmServiceImpl extends ServiceImpl<ExteriorSiteAlarmMapper, ExteriorSiteAlarm> implements IExteriorSiteAlarmService {
    
    @Autowired
    private ExteriorSiteAlarmMapper exteriorSiteAlarmMapper;

    @Autowired
    private OptPatrolEventMapper optPatrolEventMapper;

    @Override
    public ExteriorSiteAlarm selectExteriorSiteAlarmById(Long id){
        return exteriorSiteAlarmMapper.selectExteriorSiteAlarmById(id);
    }
      
    @Override
    public List<ExteriorSiteAlarm> selectExteriorSiteAlarmList(ExteriorSiteAlarm exteriorSiteAlarm){
        return exteriorSiteAlarmMapper.selectExteriorSiteAlarmList(exteriorSiteAlarm);
    }

    @Override
    public List<ExteriorSiteAlarm> getNewestAlarm() {
        return exteriorSiteAlarmMapper.getNewestAlarm();
    }

    @Override
    public List<OptDeviceAlarmDto> cockpitRiverwayAlarm(CockpitRiverwayAlarmDto dto) {
        List<ExteriorSiteAlarmVo> vos = exteriorSiteAlarmMapper.cockpitRiverwayAlarm(dto);
        if (dto.getAlarmCode() != 2){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //过滤当天数据
            Map<String, List<ExteriorSiteAlarmVo>> collect = vos.stream().filter(v -> {
                Date lastAlarmTime = v.getLastAlarmTime();
                return sdf.format(lastAlarmTime).equals(sdf.format(new Date()));
            })
                    //按最后报警时间排序
                    .sorted(Comparator.comparing(ExteriorSiteAlarmVo::getLastAlarmTime).reversed())
                    //按微站id分组
                    .collect(Collectors.groupingBy(ExteriorSiteAlarmVo::getExteriorId));
            //过滤历史数据
            List<ExteriorSiteAlarmVo> collect1 = vos.stream().filter(v -> {
                Date lastAlarmTime = v.getLastAlarmTime();
                return !sdf.format(lastAlarmTime).equals(sdf.format(new Date()));
            }).collect(Collectors.toList());
            for (String key : collect.keySet()){
                //当天最新报警直接加入集合，其余修改报警状态为历史再加入集合
                List<ExteriorSiteAlarmVo> alarmVos = collect.get(key);
                if (dto.getAlarmCode() == 0){
                    for (int i=1; i<alarmVos.size(); i++){
                        ExteriorSiteAlarmVo vo = alarmVos.get(i);
                        vo.setAlarmState(1);
                    }
                }
                collect1.addAll(alarmVos);
            }
            vos = collect1;
        }
        vos = vos.stream().sorted(Comparator.comparing(ExteriorSiteAlarmVo::getLastAlarmTime).reversed()).collect(Collectors.toList());
        List<OptDeviceAlarmDto> list = new ArrayList<>();
        List<OptPatrolEventDPY> optPatrolEventDPIES = optPatrolEventMapper.selesctDPY();
        for (ExteriorSiteAlarmVo vo : vos){
            String id = "断面" + vo.getId();
            OptDeviceAlarmDto alarmDto = new OptDeviceAlarmDto();
            for (OptPatrolEventDPY dpy : optPatrolEventDPIES){
                if (id.equals(dpy.getMinorId())){
                    vo.setBusinessId(dpy.getId());
                    vo.setActivityName(dpy.getActivityName());
                    vo.setActivityFlag(dpy.getActivityFlag());
                }
            }
            alarmDto.setSiteAlarmVo(vo);
            list.add(alarmDto);
        }
        return list;
    }

    @Override
    public boolean updateConfirmAlarmToInDisposal(String alarmId,Integer state) {
        if (ObjectUtil.isNull(alarmId)) return false;
        LambdaUpdateWrapper<ExteriorSiteAlarm> wrapper = Wrappers.lambdaUpdate();
        if (state == 1){
            wrapper.set(ExteriorSiteAlarm::getConfirmAlarm, Constant.ConfirmAlarmType.IN_DISPOSAL.getValue());
        }else {
            wrapper.set(ExteriorSiteAlarm::getConfirmAlarm, Constant.ConfirmAlarmType.CLEARED.getValue());
        }
        wrapper.eq(ExteriorSiteAlarm::getId, alarmId);
        return update(wrapper);
    }
}
